var pays={};
var ptype = 'wxpay'; 
var w=null;
var url = '';
document.addEventListener('plusready', function(){
	// 获取支付通道
	plus.payment.getChannels(function(channels){
		for(var i in channels){
			var channel=channels[i];
			pays[channel.id]=channel;
			checkServices(channel);
		}
	},function(e){
		outLine("获取支付通道失败："+e.message);
	});
	
}, false)
// 检测是否安装支付服务
function checkServices(pc){
	if(!pc.serviceReady){
		var txt=null;
		switch(pc.id){
			case "alipay":
			txt="检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
			break;
			default:
			txt="系统未安装“"+pc.description+"”服务，无法完成支付，是否立即安装？";
			break;
		}
		plus.nativeUI.confirm(txt,function(e){
			if(e.index==0){
				pc.installService();
			}
		},pc.description);
	}
}
var orderid;
//支付
function payment(money){
	console.log(money+" "+ptype);
	//预设订单Id ——> 用户Id+当前时间戳
	orderid = uid+new Date().getTime();
	if(w){return;}//检查是否请求订单中
	console.log(orderid+" "+money+" "+ptype); 
	if(ptype == 'alipay'){
//		console.log(webRoot + '/pay/Alipay.php?orderid=' + orderid + '&price='+money);return;
		url = webRoot + '/pay/Alipay.php?orderid=' + orderid + '&price='+money;
	}else if(ptype == 'wxpay'){
		console.log(webRoot + '/pay/index.php?orderid=' + orderid + '&price='+money+'&type=2');
		url = webRoot + '/pay/index.php?orderid=' + orderid + '&price='+money+'&type=2';
	}
	w = plus.nativeUI.showWaiting('支付中...',{width:'80px',height:'80px',background:'rgba(0,0,0,0.3)'});
	// 请求支付订单
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
		switch (xhr.readyState) {
			case 4:
			w.close(); 
			w = null;
			if (xhr.status == 200) { 
				var order = xhr.responseText;
				plus.payment.request(pays[ptype], order, function(result) {
					//对订单进行操作
					source();
				}, function(e) {
					toast("支付失败");
					toast("["+e.code+"]："+e.message);
				});
			} else { 
				plus.nativeUI.toast("获取订单信息失败！");
			}
			break;
		default:
			break;
		}
	}
	xhr.open('GET', url);
	xhr.send();
}